package com.handong.handserialprogram.module.base;

import android.support.v4.app.Fragment;

import com.handong.handserialprogram.R;

import java.util.ArrayList;

/**
 * Created by userKey on 2017/12/19.
 */

public abstract class BackStackActivity<P extends BasePresenter> extends BaseActivity<P> {

    private static final String TAG = BackStackActivity.class.getSimpleName();
    private Fragment mCurrentFragment;

    private static final int CURRENT_LEVEL = 1;
    private static final int LAST_LEVEL = 2;
    private static final int BACK_STACK_SIZE = 3;

    private static final int DEFAULT_IN_ANIM = R.anim.push_left_in;
    private static final int DEFAULT_OUT_ANIM = R.anim.push_right_out;
    private static final int DEFAULT_NO_ANIM = R.anim.anim_no;

    private ArrayList<BackStackFragment> mBackStackList;

    @Override
    protected P createPresenter() {
        return null;
    }

    @Override
    protected void initViewByID() {
    }

    @Override
    protected void initViewDataRes() {
    }

    protected void addFirstFragment(BackStackFragment fragment, int containerID) {
        addFirstFragment(fragment, containerID, BACK_STACK_SIZE);
    }

    protected void addFirstFragment(BackStackFragment fragment, int container, int size) {
        getSupportFragmentManager().beginTransaction()
                .add(container, fragment)
                //.addToBackStack(fragment.getClass().getSimpleName())
                .commit();
        if (mBackStackList == null) {
            mBackStackList = new ArrayList<>(size);
        }
        mCurrentFragment = fragment;
        mBackStackList.add(fragment);
    }

    protected void addFragment(BackStackFragment fragment, int containerID) {
        addFragment(fragment, containerID, R.anim.push_left_in, R.anim.anim_no);
    }

    protected void addFragment(BackStackFragment fragment, int containerID, int inAnim, int outAnim) {
        getSupportFragmentManager().beginTransaction()
                .setCustomAnimations(inAnim, outAnim)
                .hide(mCurrentFragment)
                .add(containerID, fragment)
                //.addToBackStack(fragment.getClass().getSimpleName())
                .commit();
        mCurrentFragment = fragment;
        mBackStackList.add(fragment);
    }

    public void removeFragment(BackStackFragment fragment) {
        removeFragment(fragment, DEFAULT_NO_ANIM, DEFAULT_OUT_ANIM);
    }

    public void removeFragment(BackStackFragment fragment, int inAnim, int outAnim) {
        ArrayList<BackStackFragment> list = mBackStackList;
        int count = list.size();
        if (count == CURRENT_LEVEL) {
            finish();
        } else {
            int index = count - CURRENT_LEVEL;
            BackStackFragment stackFragment = list.get(index);
            removeFragmentFromList(list, index, stackFragment, inAnim, outAnim);
        }
    }

    public BackStackFragment getIndexFragment(int index) {
        if (index >= mBackStackList.size()) {
            return null;
        }
        return mBackStackList.get(index);
    }

    @Override
    public void onBackPressed() {
        ArrayList<BackStackFragment> list = mBackStackList;
        int count = list.size();
        if (count > CURRENT_LEVEL) {
            int index = list.size() - CURRENT_LEVEL;
            BackStackFragment fragment = list.get(index);
            if (fragment.onBackClicked()) {
                return;
            }
            removeFragmentFromList(list, index, fragment);
        } else {
            finish();
        }
    }

    private void removeFragmentFromList(ArrayList<BackStackFragment> list, int index, BackStackFragment fragment) {
        removeFragmentFromList(list, index, fragment, DEFAULT_NO_ANIM, DEFAULT_OUT_ANIM);
    }


    private void removeFragmentFromList(ArrayList<BackStackFragment> list, int index, BackStackFragment fragment,
                                        int inAnim, int outAnim) {
        list.remove(index);
        mCurrentFragment = list.get(list.size() - CURRENT_LEVEL);
        getSupportFragmentManager().beginTransaction()
                .setCustomAnimations(inAnim, outAnim)
                .remove(fragment)
                .show(mCurrentFragment)
                .commit();
    }

    @Override
    protected void onDestroy() {
        mCurrentFragment = null;
        if (mBackStackList != null) {
            mBackStackList.clear();
            mBackStackList = null;
        }
        super.onDestroy();
    }
}
